<template>
  <div class="login-page">
    <cp-nav-bar title="登录" rightText="注册" @click-right="$router.push('/register')"></cp-nav-bar>
    <div class="login-head">
      <h2>{{ isPass ? '密码登录' : '短信验证码登录' }}</h2>
      <a href="javascript:;">
        <span @click="isPass = !isPass">{{ isPass ? '短信验证码登录' : '密码登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <van-form ref="form" aria-autocomplete="off" @submit="onSubmit">
      <van-field
        name="mobile"
        v-model="mobile"
        :rules="mobileRules"
        placeholder="请输入手机号"
        type="tel"
      ></van-field>
      <van-field
        v-if="isPass"
        v-model="password"
        :rules="passwordRules"
        placeholder="请输入密码"
        type="password"
      ></van-field>
      <van-field v-else v-model="code" :rules="codeRules" placeholder="短信验证码">
        <template #button>
          <span class="btn-send" :class="{ active: time > 0 }" @click="onSend">
            {{ time > 0 ? `${time}后再次发送` : '发送验证码' }}</span
          >
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="agree"></van-checkbox>
        <span>&nbsp;我已同意&nbsp;</span>
        <a href="javascript:;">用户协议</a>
        <span>&nbsp;及&nbsp;</span>
        <a href="javascript:;">隐私条款</a>
      </div>
      <div class="cp-cell">
        <van-button native-type="submit" block round type="primary">登录</van-button>
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <cp-icon name="consult-alipay"></cp-icon>
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <div class="icon">
        <img src="@/assets/QQ.png" alt="" />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onUnmounted, ref } from 'vue'
import { mobileRules, passwordRules, codeRules } from '@/utils/rules'
import { showSuccessToast, showToast, type FormInstance } from 'vant'
import { loginByPassword, sendMobileCode, loginByMobile } from '@/api/user'
import { useUserStore } from '@/stores'
import { useRoute, useRouter } from 'vue-router'

onUnmounted(() => {
  clearInterval(timer)
})

const store = useUserStore()
const router = useRouter()
const route = useRoute()

const mobile = ref('13230000001')
const password = ref('abc12345')
const code = ref('')
const agree = ref(false)

const onSubmit = async () => {
  if (!agree.value) return showToast('请勾选协议')
  const res = isPass.value
    ? await loginByPassword(mobile.value, password.value)
    : await loginByMobile(mobile.value, code.value)

  store.setUser(res.data)
  showSuccessToast('登录成功')
  router.replace((route.query.returnUrl as string) || 'user')
  console.log('登录', res)
}

// 短信登录界面切换
const isPass = ref(true)
// 发送短信验证码
const time = ref(0)
const form = ref<FormInstance>()
let timer: number
const onSend = async () => {
  if (time.value > 0) return
  await form.value?.validate('mobile')
  const res = await sendMobileCode(mobile.value, 'login')

  code.value = res.data.code
  showToast('发送成功')
  time.value = 60
  // 开启定时器
  if (timer) clearInterval(timer)
  timer = setInterval(() => {
    time.value--
    if (time.value <= 0) clearInterval(timer)
  }, 1000)
}
</script>

<style scoped lang="scss">
.login-page {
  padding: 60px 30px 0;
  .login-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .login-other {
    .icon {
      text-align: center;
      & img {
        width: 40px;
        height: 40px;
      }
    }
  }
}
.cp-cell {
  margin-top: 20px;
  display: flex;
  align-items: center;
  a {
    color: var(--cp-primary);
  }
}
.btn-send {
  color: var(--cp-primary);
  &.active {
    color: rgba(22, 194, 163, 0.5);
  }
}
</style>
